/*
 * EditorTabbedPane.java
 */

package xq.swing;

import java.io.File;
import java.io.IOException;
import java.util.ServiceLoader;
import javax.swing.JTabbedPane;

/**
 *
 * @author ThorntonRP
 */
public class EditorTabbedPane extends JTabbedPane {

	private static final long serialVersionUID = -2441737365178701979L;

	public EditorTabbedPane() {
		initComponents();
	}

	/*--------------------------------------------------------------------
	 * Field Accessors
	 *--------------------------------------------------------------------*/
	
	public Editor getActiveDocumentEditor() {
		return (Editor) getSelectedComponent();
	}
	
	public File getActiveDocumentFile() {
		return getActiveDocumentEditor().getFile();
	}
	
	public String getActiveDocumentText() {
		return getActiveDocumentEditor().getText();
	}
	
	/*--------------------------------------------------------------------
	 * File Operations
	 *--------------------------------------------------------------------*/
	
	public void openNewDocument(String contentType) {
		addDocumentEditor(contentType);
	}
	
	public void openDocument(String contentType, File file) throws IOException {
		addDocumentEditor(contentType, file);
	}
	
	public void saveActiveDocument() throws IOException {
		getActiveDocumentEditor().save();
	}
	
	public void saveActiveDocument(File f) throws IOException {
		getActiveDocumentEditor().save(f);
	}
	
	public void closeActiveDocument() {
		int index = getSelectedIndex();
		removeTabAt(index);
		if (getTabCount() > 0) {
			setSelectedIndex(Math.max(0, index - 1));
		}
	}
	
	public void revertActiveDocument() throws IOException {
		getActiveDocumentEditor().revert();
	}
	
	public boolean isActiveDocumentModified() {
		return getActiveDocumentEditor().isModified();
	}
	
	/*--------------------------------------------------------------------
	 * Edit Operations
	 *--------------------------------------------------------------------*/
	
	public void undo() {
	}
	
	public void redo() {
	}

	/*--------------------------------------------------------------------
	 * Private Operations
	 *--------------------------------------------------------------------*/
	
	private synchronized void addDocumentEditor(String contentType) {
		Editor editor = getEditorFactory(contentType).createEditor(contentType);
		addTab(editor.getName(), editor);
		setSelectedIndex(getTabCount() - 1);
	}

	private synchronized void addDocumentEditor(String contentType, File f) throws IOException {
		Editor editor = getEditorFactory(contentType).createEditor(contentType, f);
		addTab(editor.getName(), editor);
		setSelectedIndex(getTabCount() - 1);
	}
	
	private EditorFactory getEditorFactory(String contentType) {
		EditorFactory factory = null;
		for (EditorFactory ef : ServiceLoader.load(EditorFactory.class)) {
			if (ef.acceptsContentType(contentType)) {
				factory = ef;
				break;
			}
		}
		if (factory == null) {
			throw new RuntimeException("Unable to find editor for content type \"" + contentType + "\"");
		} else {
			return factory;
		}
	}

	private void initComponents() {
	}
}
